package com.tencent.org.service.impl;

import com.tencent.basic.util.Md5Utils;
import com.tencent.basic.util.PageList;
import com.tencent.basic.util.StrUtils;
import com.tencent.org.domain.Emp;
import com.tencent.org.mapper.EmpMapper;
import com.tencent.org.query.EmpQuery;
import com.tencent.org.service.EmpService;
import com.tencent.user.domain.Logininfo;
import com.tencent.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmpServiceImpl implements EmpService {
    // 注入DepartmentMapper对象
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void del(Long id) {
        Emp emp = empMapper.findById(id);
        logininfoMapper.del(emp.getLogininfo_id());
        empMapper.delEmpRoleByEmpId(id);
    }

    @Override
    @Transactional
    public void add(Emp emp) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(emp.getPassword() + salt);
        emp.setSalt(salt);
        emp.setPassword(md5Pwd);

        // 保存logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(emp, logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);

        emp.setLogininfo_id(logininfo.getId());
        empMapper.add(emp);

        empMapper.addEmpRole(emp);
    }

    @Override
    @Transactional
    public void update(Emp emp) {
        // 处理加盐加密
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(emp.getPassword() + salt);
        emp.setSalt(salt);
        emp.setPassword(md5Pwd);
        empMapper.update(emp);
        // 获取登录信息id
        Long logininfoId = empMapper.findById(emp.getId()).getLogininfo_id();
        // 创建logininfo
        Logininfo logininfo = logininfoMapper.findById(logininfoId);
        // 新数据覆盖旧数据
        BeanUtils.copyProperties(emp, logininfo);
        logininfoMapper.update(logininfo);

        empMapper.delEmpRoleByEmpId(emp.getId());
        if (emp.getRoleId() != null){
            empMapper.addEmpRole(emp);
        }

    }

    @Override
    public List<Emp> findAll() {
        return empMapper.findAll();
    }

    @Override
    public Emp findById(Long id) {
        return empMapper.findById(id);
    }

    @Override
    public PageList<Emp> queryPage(EmpQuery empQuery) {
        // 查询数据的总条数
        Integer totals = empMapper.queryCount(empQuery);
        if (totals == 0){
            return new PageList<>();
        }
        // 查询当前页的数据
        List<Emp> data = empMapper.queryData(empQuery);
        return new PageList<>(data, totals);

    }

    @Override
    public void patchDel(List<Long> ids) {
        empMapper.patchDel(ids);
    }

    @Override
    public List<Emp> empDepartmentTree() {
        List<Emp> departments = empMapper.findAll();
        // 把所有员工装在MAP中
        Map<Long, Emp> map = new HashMap<>();
        for (Emp Emp: departments) {
            // 将部门名字和部门信息装进map中
            map.put(Emp.getDepartment_id(), Emp);
        }
        // 查询部门树
        List<Emp> empDepartmentTree = new ArrayList<>();
        for (Emp emp: departments) {
            if (emp.getDepartment_id() == null){
                // 如果没有父部门id，则为顶级部门，则把他加到部门树的集合中
                empDepartmentTree.add(emp);
            }else{
                // 如果又父部门id，获取到父部门的id
                Long parent_id = emp.getDepartment_id();
                // 通过父部门的id获取到父部门
                Emp parentDepartment = map.get(parent_id);
                // 再把自己装进父部门的子集合Children中
                parentDepartment.getChildren().add(emp);
            }
        }
        return empDepartmentTree;
    }

}
